package yxy.game.pm2.active.command.challenge;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.subordinateclass.ChanllengeVideoModule;
import yxy.game.pm2.active.module.subordinateclass.ChanllengeVideoRecord;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

import java.util.HashMap;

/** 限时挑战 挑战执行 */
@MessageType(proto = Command.ActChallengeVideo, classtype = ActiveMessage.ActChallengeVideoRequest.class)
public class CMD_Active_限时挑战_ChallengeVideo extends Command<ActiveMessage.ActChallengeVideoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActChallengeVideoRequest request) {
		String playerCode = messageSource.getTag();
		int id = request.getId();

		ActiveMessage.ActChallengeVideoResponse.Builder response = ActiveMessage.ActChallengeVideoResponse.newBuilder();

		PlayerBean pb = PlayerBean.get(playerCode);
		ServerBean sb = ServerBean.get(pb.getServerId());
		
		ChanllengeVideoModule module = ChanllengeVideoModule.get(sb.getAreaId(), id);
		
		if(module.getBestpower() != null) {
			ChanllengeVideoRecord record = module.getBestpower();
			ActiveMessage.ChanllengeBest.Builder build = ActiveMessage.ChanllengeBest.newBuilder();
			PlayerBean recordpb = PlayerBean.get(record.getPlayerCode());
			
			build.setPlayerCode(recordpb.getCode());
			build.setBattleCode(record.getRecordCode());
			build.setHead(recordpb.getHead());
			build.setHeadFrame(recordpb.getHeadframe());
			build.setNickname(recordpb.getName());
			build.setValue(record.getPower());
			response.setBestpower(build.build());
		}

		if(module.getBestround().size() != 0) {
			HashMap<Integer, ChanllengeVideoRecord> records = module.getBestround();
			for(ChanllengeVideoRecord record : records.values()) {
				ActiveMessage.ChanllengeBest.Builder build = ActiveMessage.ChanllengeBest.newBuilder();
				PlayerBean recordpb = PlayerBean.get(record.getPlayerCode());
				
				build.setPlayerCode(recordpb.getCode());
				build.setBattleCode(record.getRecordCode());
				build.setHead(recordpb.getHead());
				build.setHeadFrame(recordpb.getHeadframe());
				build.setNickname(recordpb.getName());
				build.setValue(record.getRounds());
				response.addBestrounds(build.build());
			}
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActChallengeVideoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}